package com.example.nonggo;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;

import com.allen.library.RxHttpUtils;
import com.allen.library.config.OkHttpConfig;
import com.blankj.utilcode.BuildConfig;
import com.blankj.utilcode.util.Utils;
import com.example.nonggo.api.RequestLogInterceptor;
import com.example.nonggo.bean.MBaseData;
import com.example.nonggo.convert.ResultJsonDeser;
import com.example.nonggo.utils.ActivityManager;
import com.example.nonggo.utils.CustomToastUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tencent.mmkv.MMKV;

import java.util.HashMap;
import java.util.Map;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.converter.gson.GsonConverterFactory;

public class MyApp extends Application {


    @Override
    public void onCreate() {
        super.onCreate();
        Utils.init(this);
        initRxHttpUtils();
        MMKV.initialize(this);
        CustomToastUtils.init(this);
        registerActivityLifecycleCallbacks(new ActivityManager());
    }

    /**
     * 全局请求的统一配置（以下配置根据自身情况选择性的配置即可）
     */
    private void initRxHttpUtils() {


        Gson gson = new GsonBuilder()
                .registerTypeHierarchyAdapter(MBaseData.class, new ResultJsonDeser())
                .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
                .create();

        RxHttpUtils
                .getInstance()
                .init(this)
                .config()
                .setConverterFactory(GsonConverterFactory.create(gson))
                .setBaseUrl(Constants._baseUrl)
                .setOkClient(createOkHttp());


    }

    private OkHttpClient createOkHttp() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(s -> Log.w("log: ", String.format("%s", s)));
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        // 创建日志拦截器
        OkHttpClient okHttpClient = new OkHttpConfig
                .Builder(this)
                //全局超时配置
                .setReadTimeout(10)
                .setHeaders(() -> {
                    Map<String, String> map = new HashMap<>();
                    map.put("accept", "*/*");
                    map.put("accept-language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
                    map.put("priority", "u=1, i");
                    map.put("referer", "https://zhiwen-mobile.mancloud.cn/");
                    map.put("sec-fetch-dest", "empty");
                    map.put("sec-fetch-mode", "cors");
                    map.put("sec-fetch-site", "same-origin");
                    map.put("tenant-id", "1");
                    map.put("terminal", "20");
                    map.put("user-agent", "Android_zhonghua_wearos");
                    map.put("Cache-Control", "no-cache");
                    map.put("Host", "zhiwen-mobile.mancloud.cn");
                    return map;
                })
                .setAddInterceptor(interceptor)
                //全局超时配置
                .setWriteTimeout(10)
                //全局超时配置
                .setConnectTimeout(10)
                //全局是否打开请求log日志
                .setDebug(BuildConfig.DEBUG)
                .build();

        return okHttpClient;
    }

}
